package main

import (
	"bytes"
	"fmt"
	"sort"
	"text/template"

	"github.com/OT-CONTAINER-KIT/redis-operator/pkg/monitoring"
)

func main() {
	metricDescriptions := monitoring.ListMetrics()
	sort.Slice(metricDescriptions, func(i, j int) bool {
		return metricDescriptions[i].Name < metricDescriptions[j].Name
	})

	tmpl, err := template.New("Redis Operator metrics").Parse("# Operator Metrics\n" +
		"This document aims to help users that are not familiar with metrics exposed by this operator.\n" +
		"The metrics documentation is auto-generated by the utility tool \"monitoring/metricsdocs\" and reflects all of the metrics that are exposed by the operator.\n\n" +
		"## Operator Metrics List" +
		"{{range .}}\n" +
		"### {{.Name}}\n" +
		"{{.Help}} " +
		"Type: {{.Type}}.\n" +
		"{{end}}" +
		"## Developing new metrics\n" +
		"After developing new metrics or changing old ones, please run \"make generate-metricsdocs\" to regenerate this document.\n\n" +
		"If you feel that the new metric doesn't follow these rules, please change \"monitoring/metricsdocs\" according to your needs.")
	if err != nil {
		panic(err)
	}

	// generate the template using the sorted list of metrics
	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, metricDescriptions); err != nil {
		panic(err)
	}

	// print the generated metrics documentation
	fmt.Println(buf.String())
}
